<?php
namespace Depart\Controller;
use Think\Controller;

class AbnormalController extends Controller {

    public function index() {
    	$data=M('abnormal')
    			->field('aid,sn,qty,sign,s.realname as signer,reply,plan,r.realname as replyer,d.title as depart')
                ->join('LEFT JOIN bjyadmin_users as r on r.id=replyer')
                ->join('LEFT JOIN bjyadmin_type as d on d.id=departid')
               ->join('LEFT JOIN bjyadmin_users as s on s.id=signer' )
                ->select(); 
                // dump($data);die;
        $map['depart']=D('type')->where('type="depart"')->select();
        $this->assign('map',$map);
    	$this->assign('data',$data);
    	$this->display();
    }

    //添加NCR报告
    public function add_abnormal(){
    	$data=I('post.');
    	$data['sign']=time();
    	$data['signer']=$_SESSION['user']['id'];
    	$result=D('abnormal')->add($data);
    	if($result){
    		$this->success('添加成功',U('Depart/Abnormal/index'));
    	}else{
    		$this->error('添加失败');
    	}
    }
    //不合格单详情
    public function itemdetail(){
    	$get=I('get.');
    	$map['aid']=$get['id'];
    	//获取表头信息
    	$header=M('abnormal')
    			->where($map)
    			->field('aid,sn,qty,sign,s.realname as signer,reply,plan,r.realname as replyer,d.title as depart')
                ->join('LEFT JOIN bjyadmin_users as r on r.id=replyer')
                ->join('LEFT JOIN bjyadmin_type as d on d.id=departid')
                ->join('LEFT JOIN bjyadmin_users as s on s.id=signer' )
                ->select(); 
    	// dump($header);die;
        $map1['abid']=$get['id'];
        $items=D('abnormalDetail')->where($map1)
        		->field('adid,content,t.title as abtype,reason,r.title as reasontype,method')
        		->join('left join bjyadmin_type as t on t.id = typeid')
        		->join('left join bjyadmin_type as r on r.id = reasontype')
        		->select();
        // dump($items);die;
        $this->assign('items',$items);
        $this->assign('header',$header);
        $this->display();
    }
    //添加不合格内容
    public function items(){
    	$post=I('post.');
    	$get=I('get.');

    	if($post){
    		$result=D('abnormalDetail')->add($post);
    		if($result){
    			$this->success('添加成功',U('Depart/Abnormal/itemdetail'));
    		}
    	}else{
    		$type=D('type')->where('type="问题分类"')->select();
    		$this->assign('id',$get['id']);
    		$this->assign('type',$type);
    		$this->display();
    	}
    	
    }

    public function reason(){
    	$get=I('get.');
    	dump($get);
    }
}
?>